<?php

class Kd_Html_Page {

    private $_scriptBlocks = array();
    private $_startupScripts = array();
    private $_scriptReferences = array();
    private $_cssBlocks = array();
    private $_cssReferences = array();
    private $_metas = array();
    private $_keyword = '';
    private $_title = '';
    private $_navigation = NUll;
    private $_tmpScriptName = NULL;

    public function __construct() {
        
    }
    
    public function getNavigation(){
    	if(is_null($this->_navigation)){
    		$this->_navigation = new Kd_Html_Navigation();
    	}
    	return $this->_navigation;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($value) {
        $this->_title = $value;
        return $this;
    }

    public function getKeyword() {
        return $this->_keyword;
    }

    public function setKeyword($value) {
        $this->_keyword = $value;
        return $this;
    }

    public function addMeta($type, $name, $content) {
        $this->_metas[] = array('type' => $type, 'value' => $name, 'content' => $content);
        return $this;
    }

    public function getMetas() {
        return $this->_metas;
    }

    public function startStartupScript(){
        ob_start();
        return $this;
    }

    public function endStartupScript(){
        $this->_startupScripts[] = ob_get_clean();
        return $this;
    }

    public function startScriptBlock($name){
    	$this->_tmpScriptName = $name;
        ob_start();
        return $this;
    }

    public function endScriptBlock(){
    	if(is_null($this->_tmpScriptName)){
        	$this->_scriptBlocks[] = ob_get_clean();
    	}else{
    		$this->_scriptBlocks[$this->_tmpScriptName] = ob_get_clean();
    	}
    	$this->_tmpScriptName = NULL;
        return $this;
    }

    public function addScriptBlock($script, $name = NULL) {
    	if(is_null($name)){
        	$this->_scriptBlocks[] = $script;
    	}else{
    		$this->_scriptBlocks[$name] = $script;
    	}
        return $this;
    }

    public function addReferenceScript($name, $src) {
        $this->_scriptReferences[$name] = $src;
        return $this;
    }

    public function addStartupScript($script) {
        $this->_startupScripts[] = $script;
        return $this;
    }

    public function getScriptBlocks() {
        return $this->_scriptBlocks;
    }

    public function getStartupScripts() {
        return $this->_startupScripts;
    }

    public function getScriptReferences() {
        return $this->_scriptReferences;
    }

    public function getCssBlocks() {
        return $this->_cssBlocks;
    }

    public function getCssReferences() {
        return $this->_cssReferences;
    }

    public function renderMeta() {
        $result = '<meta name="keyword" content="' . $this->getKeyword() . '"/>';
        foreach ($this->getMetas() as $meta) {
            $result.= "<meta {$meta['type']}=\"{$meta['value']}\" content=\"{$meta['content']}\"/>";
        }
        return $result;
    }

    public function renderScript() {
        $result = '';
        foreach ($this->getScriptReferences() as $ref) {
            $result .= "<script language=\"javascript\" type=\"text/javascript\" src=\"$ref\"></script>\n";
        }
        if (count($this->_scriptBlocks) > 0) {
            $result .= "<script language=\"javascript\" type=\"text/javascript\">";
            foreach ($this->getScriptBlocks() as $block) {
                $result .= "\n$block";
            }
            $result .= "\n</script>\n";
        }
        if (count($this->_startupScripts) > 0) {
            $result .= "<script language=\"javascript\" type=\"text/javascript\">\njQuery(function(){";
            foreach ($this->getStartupScripts() as $startup) {
                $result .= "\n$startup";
            }
            $result .= "\n});</script>\n";
        }
        return $result;
    }

    public function renderCss() {
        $result = '';
        foreach ($this->getCssReferences() as $ref) {
            $result .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$ref}\"/>\n";
        }
        foreach ($this->getCssBlocks() as $block) {
            $result .= "<style>{$block}</style>\n";
        }
        return $result;
    }

}